﻿#include "syscfg.h"
#include "dbservice.h"


QMutex lock;

SysCfg::SysCfg()
{

}


int SysCfg::getIntValue(QString key, int defaultValue, int miniValue)
{

    lock.lock();
    QString value;
    if(f_success == gDBService.getSysSettingValue(key, value) && value.length() > 0)
    {
        bool toIntOk = false;
        int ret = value.toInt(&toIntOk);
        if(toIntOk)
        {
            lock.unlock();

            if(ret < miniValue)
            {
                ret = miniValue;
            }
            return ret;
        }
        else
        {
            lock.unlock();
            return defaultValue;
        }
    }
    else
    {
        lock.unlock();
        return defaultValue;
    }
}

QString SysCfg::getValue(QString key, QString defaultValue)
{
    lock.lock();
    QString value;
    if(f_success == gDBService.getSysSettingValue(key, value) && value.length() > 0)
    {
        lock.unlock();
        return value;
    }
    else
    {
        lock.unlock();
        return defaultValue;
    }
}

int SysCfg::saveIntValue(QString key, int value)
{
    lock.lock();
    LOG_INFO("try to save value:%d and key:%s", value, QStr2CStr(key));
    gDBService.deleteSysSettingValue(key);
    int ret = gDBService.insertSysSettingValue(key, int2QStr(value));
    lock.unlock();
    return ret;

}

int SysCfg::saveValue(QString key, QString value)
{
     LOG_INFO("try to save value:%s and key:%s", QStr2CStr(value), QStr2CStr(key));
     lock.lock();
     gDBService.deleteSysSettingValue(key);
     int ret = gDBService.insertSysSettingValue(key, value);
     lock.unlock();
     return ret;
}
